package com.lukamikec.kmap;

import com.lukamikec.kmap.kmap_kgen.marker;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.view.View.OnClickListener;
import android.view.*;
import android.widget.*;
import android.widget.ImageView.ScaleType;
import android.widget.TabHost.TabSpec;
import android.widget.TextView.OnEditorActionListener;

public class Akt_kmap extends Activity {

	ImageView preglednik;
	EditText  dno_tbox;
	
	float scrl_poc_x, scrl_poc_y;
	
	public void dialog (String str)
	{
		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("K-MAP");
		alertDialog.setMessage(str);
		alertDialog.show();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kmap);
        
        kmap_kgen kkk = null;
    	try
    	{
    		kmap_scanner kscn = new kmap_scanner (getIntent().getExtras().getString("frml"));
    		
    		kmap_expr formula_tree = 
    				new kmap_expr(
    						kscn, 
    						"("	
    						);
    		//dialog(formula_tree.draw());
 		
    		kkk = new kmap_kgen(formula_tree);
    	}
    	catch (Exception ex)
    	{
    		dialog("Error. :(");
    		Intent intent = new Intent(this, MainActivity.class);
    		startActivity(intent);
    		finish();

    	}        
      
        
		/*try {
			//kmap_scanner kscn = new kmap_scanner("A*B");
			kmap_scanner kscn = new kmap_scanner("!A + !B");

			kmap_expr formula_tree;
			formula_tree = new kmap_expr(kscn, "(");

			

			//dialog(kkk.print());
		} catch (Exception e) {

			dialog(e.getStackTrace().toString());
			
		}*/

		preglednik = (ImageView) this.findViewById(R.id.tab1);
		dno_tbox = (EditText) this.findViewById(R.id.editText1);
        
        preglednik.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View arg0, MotionEvent event) {

                float tr_x, tr_y;

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                    	scrl_poc_x = event.getX();
                    	scrl_poc_y = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        tr_x = event.getX();
                        tr_y = event.getY();
                        preglednik.scrollBy((int) (scrl_poc_x - tr_x), (int) (scrl_poc_y - tr_y));
                        if (preglednik.getScrollX() < 0)
                        	preglednik.scrollTo(0, preglednik.getScrollY());
                        if (preglednik.getScrollY() < 0)
                        	preglednik.scrollTo(preglednik.getScrollX(),0);
                        
                        
                        scrl_poc_x = tr_x;
                        scrl_poc_y = tr_y;
                        break;
                    case MotionEvent.ACTION_UP:
                    	tr_x = event.getX();
                    	tr_y = event.getY();
                    	preglednik.scrollBy((int) (scrl_poc_x - tr_x), (int) (scrl_poc_y - tr_y));
                    	if (preglednik.getScrollX() < 0)
                        	preglednik.scrollTo(0, preglednik.getScrollY());
                        if (preglednik.getScrollY() < 0)
                        	preglednik.scrollTo(preglednik.getScrollX(),0);
                     
                        break;
                }

                return true;
            }
        });

        bkmap = Bitmap.createBitmap(kkk.polja_gore * 50 + 100
        		, kkk.polja_lijevo * 50 + 100, Config.ARGB_8888);
        
        int hpreg = getWindowManager()
				.getDefaultDisplay().getHeight() - 110;
        
        if (bkmap.getHeight() < hpreg)
        	hpreg = bkmap.getHeight();
        
        //if (bkmap.getWidth() < preglednik.getLayoutParams().width)
        //	preglednik.setScaleType(ScaleType.CENTER);
        	
        
        preglednik.setLayoutParams(new LinearLayout.LayoutParams(preglednik
				.getLayoutParams().width, hpreg));

        Canvas canvas = new Canvas(bkmap);
        
		canvas.drawColor(Color.DKGRAY);
        
		Paint pnt = new Paint();
		pnt.setARGB(255, 255, 255, 255);
		pnt.setTextSize(16);
		pnt.setTextAlign(Align.CENTER);
		pnt.setAntiAlias(true);
		
		for (int k = 0; k < kkk.polja_gore; ++k)
		{
			canvas.drawText(kkk.headers_top.get(k), 
					100 + k*50, 50, pnt);
			
			canvas.drawLine(100 - 25 + k*50, 
					50, 
					100 - 25 + k*50, 
					50 * kkk.polja_lijevo + 75, 
					pnt);
		}
		for (int k = 0; k < kkk.polja_lijevo; ++k)
		{
			canvas.drawText(kkk.headers_left.get(k), 
					50, 100 + k*50, pnt);
				
			canvas.drawLine(50,
					100 - 25 + k*50, 
					50 * kkk.polja_gore + 75,
					100 - 25 + k*50, 
					pnt);
		}
		
		int pointx = 100, pointy = 100;
		
		for (int redak = 0; redak < kkk.polja_lijevo; ++redak)
		{
			for (int stupac = 0; stupac < kkk.polja_gore; ++stupac)
			{
				String out = (kkk.tablica[redak][stupac].val ? "1" : "");
				canvas.drawText(out, 
						pointx + stupac * 50, 
						pointy + redak * 50, 
						pnt);
						
			}
		}
		
		canvas.drawLine(30, 30, 75, 75, pnt);
		//String labelg = "", labell = "";
		
		
		canvas.drawText(kkk.var_header_top, 60, 30, pnt);
		canvas.drawText(kkk.var_header_left, 30, 60, pnt);
		
		String DNO = "";
		int step = 180 / kkk.markers.size();
		for (int k = 0; k < kkk.markers.size(); ++k)
		{
			marker m = kkk.markers.get(k);
			Paint boja = new Paint();
			boja.setARGB(160, (int)(Math.random() * 30 + 40 + step * k), 
					(int)(Math.random() * 255),
					(int)(Math.random() * 30 + 220 - step * k));
			
			boja.setStyle(Style.FILL);
			
			for (int redak = m.y; redak < m.y+m.h; ++redak)
			{
				for (int stupac = m.x; stupac < m.x+m.w; ++stupac)
				{
					int s = redak % kkk.polja_lijevo, r = stupac % kkk.polja_gore;
					
					canvas.drawRect(new Rect(75 + 50*r, 75 + 50*s, 75 + 50*r + 50, 75 + 50*s + 50), boja);
				}
			}
			
			DNO += Integer.valueOf(m.x).toString() + " "+
					Integer.valueOf(m.y).toString() + " "+
					Integer.valueOf(m.w).toString() + " "+
					Integer.valueOf(m.h).toString() + " ### "; 
				
			/*m.x = 85 + m.x * 50;
			m.y = 85 + m.y * 50;
			m.w = 25 + 50 * (m.w - 1);
			m.h = 25 + 50 * (m.h - 1);
			
			pnt.setStyle(Style.STROKE);
			pnt.setStrokeWidth(2);
			canvas.drawRoundRect(m.toRectF(), 2, 2, pnt);*/
		}
		pnt.setTextAlign(Align.LEFT);
		//canvas.drawText(DNO, 50, 20, pnt);
		this.dno_tbox.setText(kkk.DNO);
        this.preglednik.setImageBitmap(bkmap);

    }
    
    Bitmap bkmap;

    //@Override
    public void onDraw(Canvas canvas) {

    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_kmap, menu);
        return true;
    }
}
